﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;

namespace myMvcBlog.Areas.Admin.Controllers
{
    public class TagController : AdminBaseController
    {
        public ActionResult List(int? page)
        {
            var tags = myMvcBlog.Tag.All().OrderByDescending(t => t.AddedTime);

            ListView<Tag> lvTags = new ListView<Tag>();
            lvTags.List = tags.Skip((page ?? 0) * ItemsPerPage * 2).Take(ItemsPerPage * 2).ToList();

            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)tags.Count() / (ItemsPerPage * 2));
            pager.CurrentPage = page ?? 0;
            pager.ActionName = "List";
            pager.ControllerName = "Tag";

            lvTags.Pager = pager;

            return View(lvTags);
        }

        [HttpGet]
        public ActionResult Edit(long id)
        {
            myMvcBlog.Tag tag = myMvcBlog.Tag.SingleOrDefault(t => t.ID == id);

            return View(tag);
        }

        [HttpPost]
        public ActionResult Edit(long id, string name, string encodedName, bool isActive)
        {
            myMvcBlog.Tag tag = myMvcBlog.Tag.SingleOrDefault(t => t.ID == id);
            if (tag != null && tag.ID > 0)
            {
                tag.Name = name;
                tag.AddedTime = DateTime.Now;
                tag.IsActive = isActive;

                tag.Save();

                return RedirectToAction("List");
            }
            else
            {
                throw new KeyNotFoundException("tag with id equals: " + id.ToString() + " doesn't exists");
            }
        }

        public ActionResult Delete(long id)
        {
            var tag = myMvcBlog.Tag.SingleOrDefault(t => t.ID == id);
            tag.Delete();

            return RedirectToAction("List");
        }

        [Authorize]
        public ActionResult DeletePostTag(long postId, long tagId)
        {
            PostTag postTag = PostTag.SingleOrDefault(pt => pt.PostID == postId && pt.TagID == tagId);

            postTag.Delete();

            return null;
        }

        [Authorize]
        public ActionResult AddPostTag(string addtags, long postId)
        {
            string[] tags = addtags.Split(';');
            foreach (string tag in tags)
            {
                Tag t = Tag.SingleOrDefault(ta => ta.Name == tag);
                if (t == null || t.ID == 0)
                {
                    t = new Tag();
                    t.Name = tag;
                    t.UrlName = Server.HtmlEncode(tag);
                    t.IsActive = true;

                    t.Save();
                }

                PostTag pt = new PostTag()
                {
                    PostID = postId,
                    TagID = t.ID
                };

                pt.Save();
            }

            return null;
        }
    }
}
